package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mwj
 * @date 2021/11/27
 * @TIME:10:31
 */

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R saveSetmealDto(@RequestBody SetmealDto setmealDto) {
        setmealService.saveSetmealDto(setmealDto);
        return R.success(setmealDto);
    }

    @GetMapping("/page")
    public R findByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.findByPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R deleteIds(Long[] ids) {
        System.out.println("ids" + ids);
        log.info("ids" + ids);
        setmealService.deleteIds(ids);
        return R.success("删除成功");
    }


    //套餐管理--- 批量启售/批量停售
    @PostMapping("/status/{statusHandle}")
    public R statusByIds(@PathVariable Integer statusHandle, Long[] ids) {

        setmealService.statusByIds(statusHandle, ids);
        return R.success("添加成功");
    }

    //套餐管理--- 修改套餐 （难度）
    //回显
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    //修改
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto) {
        //setmeal修改，setmealdish先删除后新增
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");

    }

/**
 * 根据条件查询套餐数据
 * //todo 因为接口url中显示的是键值对，所以不需要加@requestbody  status=1;
 * @param setmeal
 * @return
 */
@GetMapping("/list")
public R<List<Setmeal>> list(Setmeal setmeal){


    List<Setmeal> setmeals=setmealService.findList(setmeal);
    return R.success(setmeals);
}


}
